<template>
  <div class="modal-overlay" v-if="isVisible" @click="close">
    <div class="modal-container" @click.stop>
      <div class="modal-header">
        <h2>用户注册</h2>
        <span class="close" @click="close">&times;</span>
      </div>
      <div class="modal-body">
        <form @submit.prevent="register" class="register-form">
          <div class="form-group">
            <label for="username">用户名</label>
            <input 
              type="text" 
              id="username" 
              v-model="user.username" 
              required
              placeholder="请输入用户名"
              title="请输入用户名"
              class="form-input">
          </div>
          <div class="form-group">
            <label for="email">邮箱</label>
            <input 
              type="email" 
              id="email" 
              v-model="user.email" 
              required
              placeholder="请输入邮箱"
              title="请输入邮箱"
              class="form-input">
          </div>
          <div class="form-group">
            <label for="password">密码</label>
            <input 
              type="password" 
              id="password" 
              v-model="user.password" 
              required
              placeholder="请输入密码"
              title="请输入密码"
              class="form-input">
          </div>
          <div class="form-actions">
            <button type="submit" class="submit-btn" :disabled="isLoading">
            {{ isLoading ? '注册中...' : '注册' }}
          </button>
            <button type="button" @click="close" class="cancel-btn">取消</button>
          </div>
        </form>
        <Alert 
          v-if="message"
          :type="messageType"
          :description="message"
          :closable="false"
          v-model="alertVisible"
        />
      </div>
    </div>
  </div>
</template>

<script>
import Alert from './Alert.vue';

export default {
  name: 'RegisterModal',
  components: {
    Alert
  },
  data() {
    return {
      isVisible: false,
      user: {
        username: '',
        email: '',
        password: ''
      },
      message: '',
      messageType: '',
      alertVisible: true,
      isLoading: false
    }
  },
  methods: {
    open() {
      this.isVisible = true;
      this.user = {
        username: '',
        email: '',
        password: ''
      };
      this.message = '';
      this.messageType = '';
      this.alertVisible = true;
    },
    close() {
      this.isVisible = false;
      // 重置表单数据
      this.user = {
        username: '',
        email: '',
        password: ''
      };
      this.message = '';
      this.messageType = '';
      this.alertVisible = true;
    },
    async register() {
      // 表单验证
      if (!this.validateForm()) {
        return;
      }

      this.isLoading = true;
      this.message = '';
      this.messageType = '';

      try {
        const response = await fetch('http://127.0.0.1:11451/users/', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(this.user)
        });

        const result = await response.json();

        if (response.ok) {
          this.message = '注册成功';
          this.messageType = 'success';
          this.alertVisible = true;
          // 延迟关闭弹窗
          setTimeout(() => {
            this.close();
            // 同时隐藏模态框
            this.$parent.showRegisterModal = false;
          }, 1000);
        } else {
          this.message = result.detail || '注册失败';
          this.messageType = 'error';
          this.alertVisible = true;
        }
      } catch (error) {
        this.message = '网络连接失败，请检查网络设置';
        this.messageType = 'error';
        this.alertVisible = true;
      } finally {
        this.isLoading = false;
      }
    },
    validateForm() {
      // 验证用户名
      if (!this.user.username || this.user.username.trim().length < 3) {
        this.message = '用户名至少需要3个字符';
        this.messageType = 'error';
        this.alertVisible = true;
        return false;
      }

      // 验证邮箱格式
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!this.user.email || !emailRegex.test(this.user.email)) {
        this.message = '请输入有效的邮箱地址';
        this.messageType = 'error';
        this.alertVisible = true;
        return false;
      }

      // 验证密码
      if (!this.user.password || this.user.password.length < 6) {
        this.message = '密码至少需要6个字符';
        this.messageType = 'error';
        this.alertVisible = true;
        return false;
      }

      return true;
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-container {
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  width: 90%;
  max-width: 400px;
  overflow: hidden;
  animation: modalFadeIn 0.3s ease-out;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 20px;
  background: linear-gradient(135deg, #28a745, #218838);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.close {
  color: white;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.2s;
}

.close:hover {
  transform: scale(1.1);
}

.modal-body {
  padding: 25px;
}

.register-form {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #333;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 16px;
  box-sizing: border-box;
  transition: border-color 0.3s, box-shadow 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #28a745;
  box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.1);
}

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.submit-btn, .cancel-btn {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 600;
  transition: transform 0.2s, box-shadow 0.2s;
}

.submit-btn {
  background-color: #28a745;
  color: white;
}

.submit-btn:hover {
  background-color: #218838;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.cancel-btn {
  background-color: #6c757d;
  color: white;
}

.cancel-btn:hover {
  background-color: #545b62;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

</style>